package resource 
{
	import event.ResourceEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import log.Log;
	/**
	 * ...
	 * @author carlor
	 */
	[Event(name = "LOADED_COMPLETE", type = "resource.ResourceEvent")]
	public class Resource extends EventDispatcher
	{
		private var _fileName: String;
		private var _referenceCount: int;
		private var _isLoaded: Boolean;
		private var _isFailed: Boolean;
		
		public function decrementReferenceCount(): void
		{
			_referenceCount--;
		}
		
		public function incrementReferenceCount(): void
		{
			_referenceCount++;
		}
		
		public function get referenceCount(): int
		{
			return _referenceCount;
		}
		
		public function load(fileName: String): void
		{
			_fileName = fileName;
			_isLoaded = false;
			_isFailed = false;
		}
		
		public function unLoad(): void
		{
			
		}
		
		protected function onLoadComplete(e: Event): void
		{
			try {
				_isLoaded = true;
				dispatchEvent(new ResourceEvent(ResourceEvent.LOADED_COMPLETE, this));
			}catch (e: Error) {
				Log.errorLog(e.toString());
			}
		}
		
		protected function onLoadError(e: IOErrorEvent): void
		{
			try {
				_isFailed = true;
				var re: ResourceEvent = new ResourceEvent(ResourceEvent.LOADED_FAILED, this);
				dispatchEvent(re);
			}catch (e: Error) {
				Log.errorLog(e.toString());
			}
		}
		
		protected function onLoadSecurityError(e: SecurityErrorEvent): void
		{
			_isFailed = true;
			trace(e.text);
		}
		
		public function get isLoaded(): Boolean
		{
			return _isLoaded;
		}
		
		public function get isFailed(): Boolean
		{
			return _isFailed;
		}
		
		public function get fileName(): String
		{
			return _fileName;
		}
	}

}